package com.pet.seller.controller;

import com.pet.seller.mapper.AccountMapper;
import com.pet.seller.mapper.GoodsMapper;
import com.pet.seller.mapper.OrdersMapper;
import com.pet.seller.mapper.ShopMapper;
import com.pet.seller.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexControl {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private OrdersMapper ordersMapper;

  @RequestMapping(value="/index",method = RequestMethod.GET)
   public String index(Model model) {

      return "index";  
   }

    @RequestMapping(value="/api/home",method = RequestMethod.GET)
    public String home(HttpServletRequest request,Model model) {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        GoodsExample goodsExample  = new GoodsExample();
        goodsExample.createCriteria().andEmailEqualTo(email);
        List<Goods> goods =  goodsMapper.selectByExample(goodsExample);
        model.addAttribute("goods",goods);
        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        shop.setEmail(email);
        shop.setAddress("Address");
        shop.setName("Shop Name");
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);
        return "home";
    }

    @RequestMapping(value="/api/item",method = RequestMethod.GET)
    public String item(HttpServletRequest request,Model model) {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        String id = request.getParameter("id");
        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        shop.setEmail(email);
        shop.setAddress("Address");
        shop.setName("Shop Name");
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);

        Goods good = goodsMapper.selectByPrimaryKey(Integer.parseInt(id));
        model.addAttribute("good",good);
        return "item";
    }

    @RequestMapping(value="/api/account",method = RequestMethod.GET)
    public String account(HttpServletRequest request,Model model) {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        String id = request.getParameter("id");
        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        shop.setEmail(email);
        shop.setAddress("Address");
        shop.setName("Shop Name");
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);

        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andEmailEqualTo(email);
        List<Account> accounts = accountMapper.selectByExample(accountExample);
        Account account = new Account();
        account.setAccountno("");
        account.setTotal(0.0);
        if(accounts.size()>0){
            account = accounts.get(0);
            if(account.getTotal() == null){
                account.setTotal(0.0);
            }
        }
        model.addAttribute("account",account);
        return "account";
    }

    @RequestMapping(value="/api/editshop",method = RequestMethod.GET)
    public String editshop(HttpServletRequest request,Model model) {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        GoodsExample goodsExample  = new GoodsExample();
        goodsExample.createCriteria().andEmailEqualTo(email);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        model.addAttribute("goods",goodsList);

        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        shop.setEmail(email);
        shop.setAddress("Address");
        shop.setName("Shop Name");
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);
        return "editshop";
    }

    @RequestMapping(value="/register",method = RequestMethod.GET)
    public String register(Model model) {

        return "register";
    }

    @RequestMapping(value="/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("sellerEmail");
        return "index";
    }

    @RequestMapping(value="/addgoods",method = RequestMethod.POST)
    public void addgoods(HttpServletRequest request, HttpServletResponse response, @RequestParam("picture") MultipartFile picture) throws IOException {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        String category = request.getParameter("category");
        String price = request.getParameter("price");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String picname = picture.getOriginalFilename();
        BufferedOutputStream out = new BufferedOutputStream(
                new FileOutputStream(new File("D://"+picname)));
        out.write(picture.getBytes());
        out.flush();
        out.close();
        Goods goods = new Goods();
        goods.setCategory(category);
        goods.setEmail(email);
        goods.setName(name);
        goods.setPicture(picname);
        goods.setPrice(price);
        goods.setStar("0");
        goods.setDescription(description);
        goodsMapper.insertSelective(goods);
        response.sendRedirect(request.getContextPath()+"/api/editshop");
    }

    @RequestMapping(value="/editgoods",method = RequestMethod.POST)
    public void editgoods(HttpServletRequest request,HttpServletResponse response, @RequestParam("picture") MultipartFile picture) throws IOException {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        int id = Integer.parseInt(request.getParameter("id"));
        String category = request.getParameter("category");
        String price = request.getParameter("price");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        Goods goods = new Goods();
        if(!picture.isEmpty()) {
            String picname = picture.getOriginalFilename();
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File("D://" + picname)));
            out.write(picture.getBytes());
            out.flush();
            out.close();
            goods.setPicture(picname);
        }

        goods.setId(id);
        goods.setCategory(category);
        goods.setEmail(email);
        goods.setName(name);
        goods.setPrice(price);
        goods.setDescription(description);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIdEqualTo(id);
        goodsMapper.updateByExampleSelective(goods,goodsExample);

        response.sendRedirect(request.getContextPath()+"/api/editshop");
    }

    @RequestMapping(value="/saveshopinfo",method = RequestMethod.POST)
    public void saveshopinfo(HttpServletRequest request, HttpServletResponse response, @RequestParam("mainpic") MultipartFile picture) throws IOException {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        String shopname = request.getParameter("shopname");
        String address = request.getParameter("address");
        String picname = "default.png";
        if(!picture.isEmpty()) {
            picname = picture.getOriginalFilename();
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File("D://" + picname)));
            out.write(picture.getBytes());
            out.flush();
            out.close();
        }
        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        if(shops.size()>0){
            Shop shop = shops.get(0);
            shop.setAddress(address);
            shop.setName(shopname);
            if(!picture.isEmpty()) {
                shop.setMainpic(picname);
            }
            shopMapper.updateByPrimaryKeySelective(shop);
        }else{
            Shop shop = new Shop();
            shop.setName(shopname);
            shop.setAddress(address);
            shop.setEmail(email);
            shop.setMainpic(picname);
            shopMapper.insert(shop);
        }

        response.sendRedirect(request.getContextPath()+"/api/editshop");
    }
    @RequestMapping(value="/saveaccount",method = RequestMethod.POST)
    public void saveaccount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String email = (String)request.getSession().getAttribute("sellerEmail");
        String accountNo = request.getParameter("accountNo");

        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andEmailEqualTo(email);
        List<Account> accounts = accountMapper.selectByExample(accountExample);
        if(accounts.size()>0){
            Account account = accounts.get(0);
            account.setAccountno(accountNo);
            accountMapper.updateByPrimaryKey(account);
        }else{
            Account account = new Account();
            account.setAccountno(accountNo);
            account.setEmail(email);
            accountMapper.insert(account);
        }

        response.sendRedirect(request.getContextPath()+"/api/account");
    }

    @RequestMapping(value="/api/getmoneyout",method = RequestMethod.GET)
    public void getmoneyout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String email = (String)request.getSession().getAttribute("sellerEmail");

        AccountExample accountExample = new AccountExample();
        accountExample.createCriteria().andEmailEqualTo(email);
        List<Account> accounts = accountMapper.selectByExample(accountExample);
        if(accounts.size()>0){
            Account account = accounts.get(0);
            account.setTotal(0.0);
            accountMapper.updateByPrimaryKey(account);
        }

        response.sendRedirect(request.getContextPath()+"/api/account");
    }

    @RequestMapping(value="/api/orders",method = RequestMethod.GET)
    public String orders(HttpServletRequest request,Model model) {
        String email = (String) request.getSession().getAttribute("sellerEmail");

        ShopExample shopExample = new ShopExample();
        shopExample.createCriteria().andEmailEqualTo(email);
        List<Shop> shops = shopMapper.selectByExample(shopExample);
        Shop shop = new Shop();
        shop.setEmail(email);
        shop.setAddress("Address");
        shop.setName("Shop Name");
        if(shops.size()>0) {
            shop = shopMapper.selectByExample(shopExample).get(0);
        }
        model.addAttribute("shop",shop);


        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andEmailEqualTo(email);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        List<Integer> goodsIdList = new ArrayList<>();
        for(Goods goods : goodsList){
            goodsIdList.add(goods.getId());
        }

        OrdersExample ordersExample = new OrdersExample();
        ordersExample.createCriteria().andGoodsidIn(goodsIdList);
        List<Orders> ordersList = ordersMapper.selectByExample(ordersExample);
        model.addAttribute("orders", ordersList);

        return "orders";
    }
}